// @装备接口
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const keys = require('../../config/keys');
const passport = require('passport');
const Equipment = require('../../models/Equipments/Equipment');
const Equipment_type = require('../../models/Equipments/Equipment_type');
const User_equipment = require('../../models/User/User_equipment');
const open_api = require('../../controller/open_api');
const good_api = require('../../controller/good_api');
const user_api = require('../../controller/user_api');
const equipment_api = require('../../controller/equipment_api');

// @route  GET api/equipments/types/get
// @desc   获取物品类型信息
// @access Private

router.get(
    '/types/get',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        Equipment_type.find()
            .then(equipment_type => {
                if (!equipment_type) {
                    return res.status(404).json('没有任何内容');
                }
                res.json(equipment_type);
            })
            .catch(err => res.status(404).json(err));
    }
);

// @route  Post api/equipments/types/add
// @desc 新增装备类型
// @access Private

router.post(
    '/types/add',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const Equipment_type_Fields = {};
        if (req.body.name) Equipment_type_Fields.name = req.body.name;
        if (req.body.info) Equipment_type_Fields.info = req.body.info;
        new Equipment_type(Equipment_type_Fields).save().then(equipment_type => {
            res.json(equipment_type);
        });
    }
);

// @route  POST api/equipments/types/edit/:id
// @desc   编辑装备类型信息接口
// @access Private
router.post(
    '/types/edit/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const Equipment_type_Fields = {};

        if (req.body.name) Equipment_type_Fields.name = req.body.name;
        if (req.body.info) Equipment_type_Fields.info = req.body.info;

        Equipment_type.findOneAndUpdate(
            { _id: req.params.id },
            { $set: Equipment_type_Fields },
            { new: true }
        ).then(equipment_type => res.json(equipment_type));
    }
);

// @route  POST api/equipments/types/delete/:id
// @desc   删除装备类型信息接口
// @access Private
router.delete(
    '/types/delete/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        Equipment_type.findOneAndRemove({ _id: req.params.id })
            .then(equipment_type => {
                res.json(equipment_type);
            })
            .catch(err => res.status(404).json('删除失败!'));
    }
);



// @route  Post api/equipments/add
// @desc 新增装备
// @access Private

router.post(
    '/add',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const Equipment_Fields = {};
        if (req.body.name) Equipment_Fields.name = req.body.name;
        if (req.body.info) Equipment_Fields.info = req.body.info;
        if (req.body.img) Equipment_Fields.img = req.body.img;
        if (req.body.rare_level) Equipment_Fields.rare_level = req.body.rare_level;
        if (req.body.price) Equipment_Fields.price = req.body.price;
        if (req.body.is_sell) Equipment_Fields.is_sell = req.body.is_sell;
        if (req.body.is_system_sell) Equipment_Fields.is_system_sell = req.body.is_system_sell;
        if (req.body.max_strength) Equipment_Fields.max_strength = req.body.max_strength;
        if (req.body.max_defense) Equipment_Fields.max_defense = req.body.max_defense;
        if (req.body.max_magic) Equipment_Fields.max_magic = req.body.max_magic;
        if (req.body.max_speed) Equipment_Fields.max_speed = req.body.max_speed;
        if (req.body.max_accuracy) Equipment_Fields.max_accuracy = req.body.max_accuracy;
        if (req.body.equipments_type) Equipment_Fields.equipments_type = req.body.equipments_type;
        if (req.body.task) Equipment_Fields.task = req.body.task;
        if (req.body.status) Equipment_Fields.status = req.body.status;

        new Equipment(Equipment_Fields).save().then(equipment => {
            res.json(equipment);
        });
    }
);

// @route  POST api/equipments/types/edit/:id
// @desc   编辑装备信息接口
// @access Private
router.post(
    '/types/edit/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const Equipment_Fields = {};
        if (req.body.name) Equipment_Fields.name = req.body.name;
        if (req.body.info) Equipment_Fields.info = req.body.info;
        if (req.body.img) Equipment_Fields.img = req.body.img;
        if (req.body.rare_level) Equipment_Fields.rare_level = req.body.rare_level;
        if (req.body.price) Equipment_Fields.price = req.body.price;
        if (req.body.is_sell) Equipment_Fields.is_sell = req.body.is_sell;
        if (req.body.is_system_sell) Equipment_Fields.is_system_sell = req.body.is_system_sell;
        if (req.body.max_strength) Equipment_Fields.max_strength = req.body.max_strength;
        if (req.body.max_defense) Equipment_Fields.max_defense = req.body.max_defense;
        if (req.body.max_magic) Equipment_Fields.max_magic = req.body.max_magic;
        if (req.body.max_speed) Equipment_Fields.max_speed = req.body.max_speed;
        if (req.body.max_accuracy) Equipment_Fields.max_accuracy = req.body.max_accuracy;
        if (req.body.equipments_type) Equipment_Fields.equipments_type = req.body.equipments_type;
        if (req.body.task) Equipment_Fields.task = req.body.task;
        if (req.body.status) Equipment_Fields.status = req.body.status;


        Equipment.findOneAndUpdate(
            { _id: req.params.id },
            { $set: Equipment_type_Fields },
            { new: true }
        ).then(equipment => res.json(equipment));
    }
);

// @route  delete api/equipments/delete/:id
// @desc   删除装备信息接口
// @access Private
router.delete(
    '/delete/:id',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        Equipment.findOneAndRemove({ _id: req.params.id })
            .then(equipment => {
                res.json(equipment);
            })
            .catch(err => res.status(404).json('删除失败!'));
    }
);

// @route  GET api/equipment/getallequipmentinfo
// @desc   获得所有装备信息
// @access public
router.get('/getallequipmentinfo', equipment_api.GetAllEquipmentInfo);


// @route  Post api/equipments/user/add
// @desc 新增玩家装备
// @access Private

router.post(
    '/user/add',
    passport.authenticate('jwt', { session: false }),
    (req, res) => {
        const User_Equipment_Fields = {};
        if (req.body.user) User_Equipment_Fields.user = req.body.user;
        if (req.body.equipment) User_Equipment_Fields.equipment = req.body.equipment;
        if (req.body.price_type) User_Equipment_Fields.price_type = req.body.price_type;
        if (req.body.price) User_Equipment_Fields.price = req.body.price;
        if (req.body.skill) User_Equipment_Fields.skill = req.body.skill;
        if (req.body.strength) User_Equipment_Fields.strength = req.body.strength;
        if (req.body.defense) User_Equipment_Fields.defense = req.body.defense;
        if (req.body.magic) User_Equipment_Fields.magic = req.body.magic;
        if (req.body.speed) User_Equipment_Fields.speed = req.body.speed;
        if (req.body.accuracy) User_Equipment_Fields.accuracy = req.body.accuracy;
        if (req.body.status) User_Equipment_Fields.status = req.body.status;

        new User_Equipment(User_Equipment_Fields).save().then(user_equipment => {
            res.json(user_equipment);
        });
    }
);

// @route  get api/equipments/user/get/
// @desc   获取玩家装备物品接口
// @access Private
router.get(
    '/user/get',
    passport.authenticate('jwt', { session: false }),
    user_api.getuserequipment
);



// @route  get api/equipments/user/getweapon/
// @desc   获取玩家武器装备接口
// @access Private
router.get(
    '/user/getweaponbag',
    passport.authenticate('jwt', { session: false }),
    user_api.getuserweaponbag
);

// @route  get api/equipments/user/get/
// @desc   获取玩家衣服装备接口
// @access Private
router.get(
    '/user/getclothesbag',
    passport.authenticate('jwt', { session: false }),
    user_api.getuserclothesbag
);

// @route  get api/equipments/user/getshoe/
// @desc   获取玩家衣服装备接口
// @access Private
router.get(
    '/user/getshoebag',
    passport.authenticate('jwt', { session: false }),
    user_api.getusershoebag
);

// @route  get api/equipments/user/getring/
// @desc   获取玩家戒指装备接口
// @access Private
router.get(
    '/user/getringbag',
    passport.authenticate('jwt', { session: false }),
    user_api.getuserringbag
);

// @route  get api/equipments/user/getusing/
// @desc   获取玩家装备中的装备接口
// @access Private
router.get(
    '/user/getusing',
    passport.authenticate('jwt', { session: false }),
    user_api.getuserusingbag
);

// @route  post api/equipments/user/equipon/:id
// @desc   装备玩家装备接口
// @access Private
router.post(
    '/user/equipon/:id',
    passport.authenticate('jwt', { session: false }),
    user_api.userequip
);

// @route  post api/equipments/user/equipoff/:id
// @desc   脱下玩家装备中装备的接口
// @access Private
router.post(
    '/user/equipoff/:id',
    passport.authenticate('jwt', { session: false }),
    user_api.userequipoff
);

module.exports = router;